<?php

namespace app\api\module;

use app\common\model\Goods;
use app\common\model\House as HouseModel;
use app\common\model\Server;
use app\common\model\Settings;
use think\facade\Db;
use function Symfony\Component\Translation\t;

class Service extends Common
{
    /**
     * 判断是否需要登录
     *
     * @param string $interface 待检测的接口
     * @return bool
     */
    protected function needLogin($interface)
    {
        //不需登录的接口
        $ignoreList = [
            ZFN_SERVICE_CREDIT,
            ZFN_SERVICE_TOP,
            ZFN_SERVICE_LIST,
            ZFN_SERVICE_DETAIL,
            ZFN_SERVICE_HOT_LIST,
        ];

        if (in_array($interface, $ignoreList)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 执行接口操作
     *
     * @param $interface
     * @return array|string
     */
    public function exec($interface)
    {
        switch ($interface) {
            //【1001】信用卡列表
            case ZFN_SERVICE_CREDIT:
                return $this->creditList();
            //【1002】服务入驻信息
            case ZFN_SERVICE_APPLY_INFO:
                return $this->applyInfo();
            //【1003】服务入驻提交
            case ZFN_SERVICE_APPLY_SUB:
                return $this->applySub();
            //【1004】服务咨询页顶部
            case ZFN_SERVICE_TOP:
                return $this->top();
            //【1005】服务列表
            case ZFN_SERVICE_LIST:
                return $this->serverList();
            //【1006】服务详情
            case ZFN_SERVICE_DETAIL:
                return $this->serverDetail();
            //【1007】热门服务列表
            case ZFN_SERVICE_HOT_LIST:
                return $this->hotList();
            //【1008】服务分享
            case ZFN_SERVICE_SHARE:
                return $this->serverShare();
            //【1009】拨打服务电话
            case ZFN_SERVICE_CALL:
                return $this->call();
            default:
                send_developer_error("模块【服务】中不存在接口【" . $interface . "】,核实文档后重试!");
        }
    }

    private function creditList()
    {
        $data = Settings::getSettings(ZFN_SETTINGS_CREDIT);
        $this->insertResponse('data',$data);
        return $this->response('信用卡列表');
    }

    private function applyInfo()
    {
        $user = $this->currentUser();
        $data = Server::applyInfo($user);
        $this->insertResponse('',$data);
        return $this->response('服务入驻申请信息');
    }

    private function applySub()
    {
        $user = $this->currentUser();
        $data = [
            'imgs' => $this->essentialParam('imgs'),
            'title' => $this->essentialParam('title'),
            'mobile' => $this->essentialParam('mobile'),
            'pro_id' => $this->essentialParam('pro_id'),
            'city_id' => $this->essentialParam('city_id'),
            'area_id' => $this->essentialParam('area_id'),
            'longitude' => $this->essentialParam('longitude'),
            'latitude' => $this->essentialParam('latitude'),
            'address' => $this->essentialParam('address'),
            'description' => $this->essentialParam('description'),
            'license' => $this->essentialParam('license'),
            'category_str' => $this->essentialParam('category_str'),
        ];
        Server::applySub($user,$data);
        return $this->response('入驻申请已提交，请耐心等待审核');
    }

    private function top(){
        $data = Server::top();
        $this->insertResponse('',$data);
        return $this->response('服务咨询页面顶部');
    }

    private function serverList(){
        $longitude = $this->optionalParam('longitude');
        $latitude = $this->optionalParam('latitude');
        $category_id = $this->optionalParam('category_id');
        $keyword = $this->optionalParam('keyword');
        $city_id = $this->optionalParam('city_id');
        $filter_type = $this->optionalParam('filter_type');
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = Server::getList($longitude,$latitude,$category_id,$keyword,$city_id,$filter_type,$page,$page_size);
        $this->insertResponse('data',$data);
        return $this->response('服务公司列表');
    }

    private function serverDetail(){
        $server_id = $this->essentialParam('server_id');
        $data = Server::detail($server_id);
        $this->insertResponse('',$data);
        return $this->response('服务详情');
    }

    private function hotList(){
        $server_id = $this->optionalParam('server_id');
        $longitude = $this->optionalParam('longitude');
        $latitude = $this->optionalParam('latitude');
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = Server::hotList($server_id,$longitude,$latitude,$page,$page_size);
        $this->insertResponse('data',$data);
        return $this->response('服务详情');
    }

    private function serverShare(){
        $user = $this->currentUser(true);
        $server_id = $this->essentialParam('server_id');
        $data = Server::share($user,$server_id);
        $this->insertResponse('',$data);
        return $this->response('服务分享信息');
    }

    private function call(){
        $user = $this->currentUser(true);
        $server_id = $this->essentialParam('server_id');
        Server::callMobile($user,$server_id);
        return $this->response('拨打成功');
    }
}